#pragma once

#include <iostream>
using namespace std;

template<class K>
struct BSTNode
{
	BSTNode<K>* _left;
	BSTNode<K>* _right;

	K _key;

	BSTNode(const K& key)
		:_key(key)
		,_left(nullptr)
		,_right(nullptr)
	{}
};

template <class K>
class BSTree
{
	typedef BSTNode<K> Node;
public:
	BSTree() = default;

	BSTree(const BSTree<K>& t)
	{
		_root = Copy(t._root);
	}

	~BSTree()
	{
		Destory(_root);
	}

	BSTree<K>& operator=(BSTree<K> t)
	{
		swap(_root, t._root);

		return *this;
	}

	bool find(const K& key)
	{
		if (_root == nullptr)
		{
			return false;
		}

		Node* cur = _root;
		

		while (cur)
		{
			if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{
				return true;
			}
		}

		return false;
	}

	bool findr(const K& key)
	{
		return _findr(_root, key);
	}

	bool insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;

		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(key);

		if (parent->_key > key)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}

		return true;
	}

	bool erase(const K& key)
	{
		if (_root == nullptr)
		{
			return false;
		}

		Node* parent = nullptr;
		Node* cur = _root;

		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_right;
					}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_right;
						}
						else
						{
							parent->_right = cur->_right;
						}
					}

					delete cur;
				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_left;
					}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_left;
						}
						else
						{
							parent->_right = cur->_right;
						}
					}

					delete cur;
				}
				else
				{
					Node* prightmin = cur;
					Node* rightmin = cur->_right;

					while (rightmin->_left)
					{
						prightmin = rightmin;
						rightmin = rightmin->_left;
					}

					cur->_key = rightmin->_key;

					if (prightmin->_left == rightmin)
					{
						prightmin->_left = rightmin->_right;
					}
					else
					{
						prightmin->_right = rightmin->_right;
					}

					delete rightmin;
				}

				return true;
			}
		}

		return false;
	}

	bool insertr(const K& key)
	{
		return _insert(_root, key);
	}

	bool eraser(const K& key)
	{
		return _erase(_root, key);
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
private:
	bool _findr(Node* root, const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}

		if (root->_key == key)
		{
			return true;
		}

		if (root->_key > key)
		{
			return _findr(root->_left, key);
		}
		else
		{
			return _findr(root->_right, key);
		}
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}

	bool _insert(Node*& root, const K& key)
	{
		if (root == nullptr)
		{
			root = new Node(key);
			return true;
		}

		if (root->_key > key)
		{
			return _insert(root->_left,key);
		}
		else if (root->_key < key)
		{
			return _insert(root->_right,key);
		}
		else
		{
			return false;
		}
	}

	bool _erase(Node*& root, const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}

		if (root->_key > key)
		{
			return _erase(root->_left, key);
		}
		else if (root->_key < key)
		{
			return _erase(root->_right,key);
		}
		else
		{
			if (root->_left == nullptr)
			{
				Node* del = root;

				root = root->_right;

				delete del;
				return true;
			}
			else if (root->_right == nullptr)
			{
				Node* del = root;

				root = root->_left;

				delete del;
				return true;
			}
			else
			{
				Node* rightmin = root->_right;

				while (rightmin->_left)
				{
					rightmin = rightmin->_left;
				}

				swap(root->_key, rightmin->_key);

				return _erase(root->_right, key);
			}
		}
	}


	void Destory(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		Destory(root->_left);
		Destory(root->_right);

		delete root;
	}

	Node* Copy(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}

		Node* newnode = new Node(root->_key);

		newnode->_left = Copy(root->_left);
		newnode->_right = Copy(root->_right);

		return newnode;
	}

	Node* _root = nullptr;
};